define(['jquery','common'], function($,common) {
	
    var html_content = [];
    
    html_content.push("<div class=\"mesinfobox\">");
    html_content.push("   <div class=\"popBox\">");
	html_content.push("  	 <div class=\"hd\">");
    html_content.push("  		<span class=\"title\">消息标题</span><i class=\"iconfont mes-close\">&#xe60c;</i>");	
    html_content.push("  	</div>");	
   	html_content.push("  	<div class=\"bd\" id=\"msg_content\">");
   	//中间是提醒消息
   	html_content.push("  	</div>");				
   	html_content.push("  	<div class=\"ft\">");		
   	html_content.push("  		<div class=\"page\" id=\"msg_mum\">");
   	html_content.push("  			<i class=\"iconfont pre\">&#xe613;</i><span class=\"num\" id=\"msg_num_info\" >2/3</span><i class=\"iconfont next\">&#xe658;</i>");
	html_content.push("  		</div>");
	html_content.push("  		<div class=\"btn\"><button class=\"btn btn-primary okgo\">已处理</button></div>");
	html_content.push("  	</div>");
	html_content.push("	  </div>");
    html_content.push("</div>");
    var curNo=1;
    var rela_id;//关联ID
    var cur_id;//当前消息ID
    var module_type;
    
    var getNextData = function(callback){
		common.common_ajax_request('/api/v1/show_msg',{pageNo:curNo,rela_id:rela_id,module_type:module_type},true,function(data,code,msg){
			if(msg.businessCode==0){
				_setMsgContent(data,callback);
			}else{
				$('.mes-container').css('display','none');
				layer.msg(msg.businessNote,
						   {time: 500}, //如果不配置，默认是3秒
					       function(){
         		    	   callback();
         		       }
				);
			}
		},"","post");
    }
    
    //消息处理,处理单条消息,多条消息,内部使用
	var _msgHandle = function(id,callback){
		$(".okgo").attr("disabled","disabled");
		common.common_ajax_request('/api/v1/do_msg',{id:id,pageNo:curNo,rela_id:rela_id,module_type:module_type},true,function(data,code,msg){
			if(msg.businessCode==2){
				$('.mes-container').css('display','none');
				layer.msg('消息已全部处理完成',
						   {time: 500}, //如果不配置，默认是3秒
					       function(){
         		    	   callback();
         		       }
				);
			}else if(msg.businessCode==0){
				_setMsgContent(data);
			}else{
				window.layer.msg(msg.businessNote);
			}
		},"","post",function(){
			$(".okgo").removeAttr("disabled");
		});
	}
	
	 /**
	  * 设置消息
	  */
	
	 function _setMsgContent(data,callback){
		$("#msg_content").html(data.content);
		cur_id = data.id;
		curNo = parseInt(data.pagenum);
		if(data.totalCount>1){
			$("#msg_mum").show();
			$("#msg_num_info").html(data.pagenum+"/"+data.totalCount);
			if(curNo <= parseInt(data.totalCount) && curNo !=1){
				$(".pre").show();
			}else{
				$(".pre").hide();
			}
			if(parseInt(data.totalCount) > 1 && curNo < parseInt(data.totalCount)){
				$(".next").show();
			}else{
				$(".next").hide();
			}
		}else{
			$("#msg_mum").hide();
		}
		$(".okgo").removeAttr("disabled");

	}
	/**
	 * 点击弹出框
	 */

	var _messageDialog = function(obj,callback){
		curNo = 1;
		rela_id = $(obj).attr("data-rela-id");
		module_type = $(obj).attr("data-module-type");
		common.common_ajax_request('/api/v1/show_msg',{pageNo:curNo,rela_id:rela_id,module_type:module_type},true,function(data,code,msg){
			if(msg.businessCode==2){
				$('.mes-container').css('display','none');
				layer.msg('已无信息需要处理',
						   {time: 500}, //如果不配置，默认是3秒
					       function(){
            		    	   callback();
            		       }
				);
			}else if(msg.businessCode==0){
				_initdata(data.pagenum,data.totalCount);
				_setMsgContent(data,callback);
			}else{
				$('.mes-container').css('display','none');
				layer.msg(msg.businessNote,
						   {time: 500}, //如果不配置，默认是3秒
					       function(){
							   callback();
         		       }
				);
			}
		});
		
		function _initdata(pagenum,totalCount){
			$("#msg_dialog").html(html_content.join(""));
			var e = {};
			e.f = false;
			$("body").off('click').on('click',function(event){
		    	if($('.mes-container').css("display")=='none'){	
		    	}
		    	else{
		    		if(!e.f){
		    			$('.mes-container').css('display','none');
		    		}else{
		    			e.f = false;
		    		}
		    	}
		    });  
			
			$('.mes-container').off('click').click(function (event) {
				e.f = true; 
			});
			
			$('.okgo').off('click').click(function (event) {
				_msgHandle(cur_id,callback);
			})
			
			$('.pre').off('click').click(function (event) {
				curNo--;
				getNextData(callback);
			})
			$('.next').off('click').click(function (event) {
				curNo++;
				getNextData(callback);
			})
			
			
		    $(".mes-close").off('click').on('click',function(event){
		    	$('.mes-container').css('display','none');
		    });
	
		    if($('.mes-container').css("display")=='none'){
		    	$('.mes-container').css('display','block');
			}
			var topA=$(obj).offset().top;
			var leftA=$(obj).offset().left;
			$(".mes-container").css('top',topA+13);
			$(".mes-container").css('left',leftA+3);
		}
	}
	

	return {
		//1订单,2订阅,3商品,4用户
		msgDialog:function(obj,callback){
			_messageDialog(obj,callback);
		}
	}
});
